lower = "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec".replace(
    ' ', '').split(',')
higher = "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou".replace(
    ' ', '').split(',')
for _ in range(int(input())):
    s = input()
    if s == 'tret':
        print(0)
    elif s.isdigit():
        n = int(s)
        ans = []
        if n:
            if n % 13:  # 不要尾缀0
                ans.append(lower[n % 13 - 1])
            n //= 13
        else:
            ans.append('tret')
        if n % 13:  # 其实 n 就可以
            ans.append(higher[n % 13-1])
        print(' '.join(ans[::-1]))
    else:
        i = 0
        ans = -1
        for c in higher:
            i += 1
            if c == s:
                ans = i
        if ans != -1:
            print(ans*13)
        else:
            arr = s.split()
            i = 0
            if len(arr) == 2:  # 两位数
                for c in higher:
                    i += 1
                    if c == arr[0]:
                        break
                num = i * 13
                i = 0
                for c in lower:
                    i += 1
                    if c == arr[1]:
                        break
                num += i
                print(num)
            else:  # 一位数
                ans = -1
                for c in lower:
                    i += 1
                    if c == s:
                        ans = i
                if ans != -1:
                    print(ans)
# 如果从后往前处理就不需要判一位两位
